import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../common/routes.dart';
import '../controllers/community_controller.dart';

class CommunityPage extends GetView<CommunityController> {
  const CommunityPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        surfaceTintColor: Colors.white,
        title: Image.asset(
          'assets/images/eyepetizer.png',
          fit: BoxFit.contain,
          width: 120,
          height: 25,
        ),
        centerTitle: true,
      ),
      body: GetBuilder<CommunityController>(builder: (_) {
        if (controller.isLoadingData) {
          return Center(
            child: Container(
              margin: const EdgeInsets.all(8.0),
              width: 64.0,
              height: 64.0,
              child: const CircularProgressIndicator(),
            ),
          );
        }
        return ListView.builder(
          controller: controller.scrollController,
          itemCount: controller.videoList.length + 1,
          itemBuilder: (context, index) {
            if (index == controller.videoList.length) {
              if (controller.isLoadingMore) {
                // Show loading indicator
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Center(child: CircularProgressIndicator()),
                );
              } else if (!controller.hasMore) {
                // No more data
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Center(child: Text('没有更多数据了')),
                );
              } else {
                return SizedBox.shrink();
              }
            }

            final item = controller.videoList[index];
            return GestureDetector(
              child: Container(
                color: Colors.white,
                margin: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
                child: Column(
                  children: [
                    Stack(
                      children: [
                        CachedNetworkImage(
                          width: double.infinity,
                          height: 200,
                          fadeInDuration: const Duration(seconds: 0),
                          fadeOutDuration: const Duration(seconds: 0),
                          fit: BoxFit.cover,
                          imageUrl: item.data.cover?.feed ?? '',
                          placeholder: (context, url) {
                            return Image.asset(
                              "assets/images/placeholder.png",
                              fit: BoxFit.cover,
                            );
                          },
                        ),
                        Positioned(
                          left: 0,
                          top: 0,
                          child: Container(
                            decoration: BoxDecoration(
                              color: Colors.red,
                              borderRadius: BorderRadius.only(
                                bottomRight: Radius.circular(16),
                              ),
                            ),
                            width: 46,
                            height: 22,
                            child: Padding(
                              padding: EdgeInsets.symmetric(horizontal: 8, vertical: 1),
                              child: Text(
                                item.data.category ?? '',
                                style: TextStyle(color: Colors.white, fontSize: 13),
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                    Row(
                      children: [
                        ClipRRect(
                          borderRadius: BorderRadius.circular(20),
                          child: CachedNetworkImage(
                            fit: BoxFit.cover,
                            fadeInDuration: const Duration(milliseconds: 100),
                            fadeOutDuration: const Duration(seconds: 0),
                            imageUrl: item.data.author?.icon ?? '',
                            width: 40,
                            height: 40,
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  item.data.author?.name ?? '',
                                  style: TextStyle(fontSize: 18),
                                ),
                                Text(
                                  item.data.author?.description ?? '',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Colors.black.withAlpha(127),
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
              onTap: () {
                Get.toNamed(Routes.VIDEO_DETAIL, arguments: {"id": item.data.id, "playUrl": item.data.playUrl});
              },
            );
          },
        );
      }),
    );
  }
}
